
package com.jf.cloud.platform.controller.platform;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.jf.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zz
 * @date 2022/9/6 14:22
 */
@RestController("platformShopStatisticsController")
@RequestMapping("/p/shop_statistics")
@Tag(name = "platform-店铺统计信息")
public class ShopStatisticsController {

    @Autowired
    private ShopDetailFeignClient shopDetailFeignClient;

    @Autowired
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @GetMapping("/count_by_shop_status")
    @Operation(summary = "根据店铺状态查询店铺数量" , description = "根据店铺状态查询店铺数量")
    public ServerResponseEntity<Long> countByShopStatus(@RequestParam(value = "shopStatus", required = false, defaultValue = "3") Integer shopStatus) {
        // 商家店铺数量
        ServerResponseEntity<List<Long>> shopIdsResponseEntity = shopDetailFeignClient.getShopIdsByShopStatus(shopStatus);
        if (shopIdsResponseEntity.isFail()) {
            throw new LuckException(shopIdsResponseEntity.getMsg());
        }
        List<Long> merchantShopIdList = shopIdsResponseEntity.getData();
        long merchantShopCount = 0L;
        if (CollUtil.isNotEmpty(merchantShopIdList)) {
            merchantShopCount = (long) merchantShopIdList.size();
        }
        // 供应商店铺数量
        ServerResponseEntity<List<Long>> supplierIdsResponseEntity = supplierDetailFeignClient.getSupplierIdsBySupplierStatus(shopStatus);
        if (supplierIdsResponseEntity.isFail()) {
            throw new LuckException(supplierIdsResponseEntity.getMsg());
        }
        List<Long> supplierIdList = supplierIdsResponseEntity.getData();
        long supplierShopCount = 0L;
        if (CollUtil.isNotEmpty(supplierIdList)) {
            supplierShopCount = (long) supplierIdList.size();
        }
        return ServerResponseEntity.success(merchantShopCount + supplierShopCount);
    }
}
